import dayjs from 'dayjs';

export const hasDataAndBack = (data, total, pageNo) => {
  let res = false;
  // 判断数据为空，但总数不为空
  if (data.length === 0 && total !== 0) {
    if (pageNo > 1) {
      res = true;
    }
  }
  return res;
};

export const isEmpty = (value) => {
  // 判断 null 或 undefined
  if (value === null || value === undefined) {
    return true;
  }
  // 判断数组或字符串长度为0
  if (Array.isArray(value) || typeof value === 'string') {
    return value.length === 0;
  }
  // 判断对象属性个数为0
  if (typeof value === 'object' && Object.keys(value).length === 0) {
    return true;
  }
  // 判断 NaN
  if (Number.isNaN(value)) {
    return true;
  }
  // 其他情况
  return false;
};

export function throttle(func, delay) {
  let timer = null;
  return function () {
    if (!timer) {
      timer = setTimeout(() => {
        // eslint-disable-next-line prefer-rest-params
        func.apply(this, arguments);
        timer = null;
      }, delay);
    }
  };
}

export function debounce(func, delay) {
  let timer = null;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(() => {
      // eslint-disable-next-line prefer-rest-params
      func.apply(this, arguments);
    }, delay);
  };
}
/**
 * 日期格式化,把yyyy-mm-dd转换为yyyy-mm-dd 00:00:00 或者yyyy-mm-dd 23:59:59
 * @param startDate 开始日期
 * @param endDate 结束日期
 * @returns
 */
export function dateFormatter(startDate: string, endDate: string) {
  const sDate = !startDate
    ? ''
    : dayjs(startDate).startOf('day').format('YYYY-MM-DD HH:mm:ss');
  const eDate = !endDate
    ? ''
    : dayjs(endDate).endOf('day').format('YYYY-MM-DD HH:mm:ss');

  return `${sDate},${eDate}`;
}
